package com.sinry.middleware.messages.producer;

import com.sinry.message.enums.RabbitQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author linxinlei
 * @project sinry-cloud
 * @description 默认生产者
 * @date 2022/8/7
 **/
@Slf4j
@Service
public class  SwaggerMessageProducer implements MessageProducer {

    @Resource
    protected RabbitTemplate rabbitTemplate;

    @Override
    public boolean inQueue(RabbitQueue rabbitQueue) {
        return RabbitQueue.SWAGGER_REPORT.equals(rabbitQueue);
    }

    @Override
    public void send(Object data) {
        rabbitTemplate.setReturnsCallback(this);
        rabbitTemplate.setConfirmCallback(this);
        RabbitQueue.SWAGGER_REPORT.send(rabbitTemplate, data);
    }

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        log.info("swagger producer return message: {}", returned.getMessage());
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println(correlationData);
        log.info("default producer confirm: ack {}, cause {}", ack, cause);
    }
}
